package service.impl;

import dao.sysUser.SysUserMapper;
import org.springframework.context.annotation.Scope;
import pojo.SysUser;
import service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 崔书健
 * @date 2024/7/19
 */
@Transactional
//@Scope("prototype")
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;
    @Override
    public List<SysUser> getList(SysUser sysUser) {
        try {
            return sysUserMapper.selectSysUserList(sysUser);
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 保存用户
     *
     * @param sysUser
     * @return
     */
    @Override
    public boolean add(SysUser sysUser) {
        boolean result = false;
        try {
            if (sysUserMapper.add(sysUser) == 1){
                result = true;
                // 测试事务回滚时，打开注释
                throw new RuntimeException();
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    public int upd(SysUser sysUser) {
        return sysUserMapper.upd(sysUser);
    }

    @Override
    public int del(Integer id) {
        return sysUserMapper.del(id);
    }

    @Override
    public SysUser login(String account, String password) {
        return sysUserMapper.queryAccountAndPwd(account, password);
    }

    /**
     * 分页查询用户信息列表
     *
     * @param pageIndex 页码
     * @param pageSize  页面数据量
     * @param realName
     * @param roleId
     * @return 用户信息列表
     */
    @Override
    public List<SysUser> selectPage(Integer pageIndex, Integer pageSize, String realName, Integer roleId) {
        return sysUserMapper.selectPageList(realName,roleId,pageIndex,pageSize);
    }

    /**
     * 获取用户数据总量
     *
     * @param queryRealName
     * @param queryRoleId
     * @return
     */
    @Override
    public int count(String queryRealName, Integer queryRoleId) {
        return sysUserMapper.count(queryRealName,queryRoleId);
    }

    public SysUserMapper getSysUserMapper() {
        return sysUserMapper;
    }

    public void setSysUserMapper(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }
}
